﻿using AntdUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Super.Admin.Utils
{
    public class ResourceHelper
    {
        public static readonly Lazy<ResourceHelper> _instance = new Lazy<ResourceHelper>(() => new ResourceHelper());
        public static ResourceHelper Instance => _instance.Value;
        private ResourceHelper() { }

        /// <summary>
        /// 加载图标
        /// </summary>
        /// <param name="resourcePath"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="FileNotFoundException"></exception>
        public Icon LoadIcon(string resourcePath)
        {
            if (string.IsNullOrEmpty(resourcePath)) throw new ArgumentNullException(nameof(resourcePath));
            // 获取当前程序集
            var assembly = Assembly.GetExecutingAssembly();
            // 尝试从资源流加载
            using (var stream = assembly.GetManifestResourceStream(resourcePath))
            {
                if (stream == null)
                    throw new FileNotFoundException($"资源 '{resourcePath}' 未找到");

                return new Icon(stream);
            }
        }
        /// <summary>
        /// 加载字体
        /// </summary>
        public (IntPtr fontPtr, int fontLength) LoadFont(string resourcePath)
        {
            try
            {
                if (string.IsNullOrEmpty(resourcePath)) throw new ArgumentNullException(nameof(resourcePath));
                var assembly = Assembly.GetExecutingAssembly();
                var fontStream = assembly.GetManifestResourceStream(resourcePath);
                if (fontStream != null)
                {
                    byte[] fontData = new byte[fontStream.Length];
                    fontStream.Read(fontData, 0, (int)fontStream.Length);
                    fontStream.Close();
                    IntPtr fontPtr = Marshal.AllocCoTaskMem(fontData.Length);
                    Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
                    return (fontPtr, fontData.Length);
                }
                throw new FileNotFoundException($"资源 '{resourcePath}' 未找到");
            }
            catch (Exception ex)
            {
                throw new Exception($"查找字体图标报错：" + ex.Message);
            }
        }

        public Image LoadImage(string resourcePath)
        {
            if (string.IsNullOrEmpty(resourcePath)) throw new ArgumentNullException(nameof(resourcePath));
            // 获取当前程序集
            var assembly = Assembly.GetExecutingAssembly();
            // 尝试从资源流加载
            using (var stream = assembly.GetManifestResourceStream(resourcePath))
            {
                if (stream == null)
                    throw new FileNotFoundException($"资源 '{resourcePath}' 未找到");
                return Image.FromStream(stream, true, true) ;
            }
        }
    }
}
